package com.example.java.designpattern.flyweight;

import com.example.java.designpattern.Divisor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zengty
 * @apiNote 享元模式
 * @date 2024/8/30
 * @project study
 */
public abstract class ConcreteFlyweightFactory implements FlyweightFactory {

    private final Map<String, Divisor> divisorsStore;

    protected ConcreteFlyweightFactory() {
        divisorsStore = new HashMap<>();
    }

    @Override
    public Divisor getDivisor(String divisorName) {
        if (divisorsStore.containsKey(divisorName)) {
            return divisorsStore.get(divisorName);
        }else if (isSingleton(divisorName)) {
            Divisor divisor = createSingleton(divisorName);
            divisorsStore.put(divisorName, divisor);
            return divisor;
        } else {
            return createPrototype(divisorName);
        }
    }

    protected abstract Divisor createPrototype(String divisorName);

    protected abstract boolean isSingleton(String divisorName);

    protected abstract Divisor createSingleton(String divisorName);
}
